Imports System.Collections.Generic
Imports System.Linq
Imports System.Text

Imports System.Linq
Imports System.Linq.Expressions

Public Class DbfTable(Of TRecord As {DbfRecord, New})
	Inherits DbfDotNet.DbfTable(Of TRecord)
	Implements IQueryProvider
	Implements IQueryable(Of TRecord)
	Private mExpression As System.Linq.Expressions.Expression

	Public Sub New(filepath As String, encoding As System.Text.Encoding, version As DbfVersion)
		MyBase.New(filepath, encoding, version)
		mExpression = Expression.Constant(Me)
	End Sub

	#Region "IQueryProvider Members"

	Private Function IQueryProvider_CreateQuery(expression As System.Linq.Expressions.Expression) As IQueryable(Of TElement) Implements IQueryProvider(Of TElement).CreateQuery
		If GetType(TRecord) Is GetType(TElement) Then
			Return DirectCast(New DbfQuery(Of TRecord)(Me, expression), IQueryable(Of TElement))
		End If
		Throw New Exception("TElement should be TRecord")
	End Function

	Private Function IQueryProvider_CreateQuery(expression As System.Linq.Expressions.Expression) As IQueryable Implements IQueryProvider.CreateQuery
		Return New DbfQuery(Of TRecord)(Me, expression)
	End Function

	Private Function IQueryProvider_Execute(expression As System.Linq.Expressions.Expression) As TResult Implements IQueryProvider(Of TResult).Execute
		Throw New NotImplementedException()
	End Function

	Private Function IQueryProvider_Execute(expression As System.Linq.Expressions.Expression) As Object Implements IQueryProvider.Execute
		Throw New NotImplementedException()
	End Function

	#End Region

	#Region "IEnumerable<TRecord> Members"

	Private Function IEnumerable_GetEnumerator() As IEnumerator(Of TRecord) Implements IEnumerable(Of TRecord).GetEnumerator
		For i As UInt32 = 0 To RecordCount - 1
			Dim r As TRecord = Me.GetRecord(i)
			yield Return r
		Next
	End Function

	'private new TRecord GetRecord(int i)
	'{
	'    throw new NotImplementedException();
	'}

	'private new int RecordCount
	'{
	'    get{ return 0; } 
	'}

	#End Region

	#Region "IEnumerable Members"

	Private Function System_Collections_IEnumerable_GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
		For i As UInt32 = 0 To RecordCount - 1
			Dim r As TRecord = Me.GetRecord(i)
			yield Return r
		Next
	End Function

	#End Region

	#Region "IQueryable Members"

	Private ReadOnly Property IQueryable_ElementType() As Type Implements IQueryable.ElementType
		Get
			Throw New NotImplementedException()
		End Get
	End Property

	Private ReadOnly Property IQueryable_Expression() As System.Linq.Expressions.Expression Implements IQueryable.Expression
		Get
			Return mExpression
		End Get
	End Property

	Private ReadOnly Property IQueryable_Provider() As IQueryProvider Implements IQueryable.Provider
		Get
			Return Me
		End Get
	End Property

	#End Region

End Class
